<template>
    <el-container>
        <el-main class="wrapper">
            <section class="panel cms-content">
                <div class="panel-body">
                    <div class="cms-btn-group">
                        <el-button type="success" icon="fa fa-refresh" @click="list()" size="small"> 刷新</el-button>
                        <el-button type="primary" icon="fa fa-plus" @click="create()" size="small"> 新增</el-button>
                    </div>
                    <div class="cms-search-group">
                        <div class="cms-search-item">
                            <el-input  size="medium" v-model="params.kwd" placeholder="请输入菜品名称" clearable class="input-with-select">
                                <el-button type="primary" @click="search()" slot="append" icon="el-icon-search"></el-button>
                            </el-input>
                        </div>
                        <div class="cms-search-item" style="width: 135px">
                            <el-select size="medium" v-model="params.shopid">
                                <el-option label="店铺(全部)" value=""></el-option>
                                <el-option v-for="(shop,index) in shops" :key="index" :label="shop.name"
                                           :value="shop.id"></el-option>
                            </el-select>
                        </div>
                    </div>
                    <el-table ref="multipleHandle"
                              v-loading="loading"
                              element-loading-text="努力加载中..."
                              element-loading-spinner="el-icon-loading"
                              tooltip-effect="dark" :data="listData.data"  border>
                        <el-table-column align="center" prop="id" label="编号" width="60"></el-table-column>
                        <el-table-column prop="thumb" label="封面" width="120">
                            <template slot-scope="scope">
                                <el-popover placement="right" trigger="click">
                                    <img :src="scope.row.thumb"/>
                                    <el-button slot="reference" style="border: none;height: 50px;padding: 0;overflow: hidden">
                                        <img title="点击查看原图" alt="点击查看原图" :src="scope.row.thumb"
                                             style="height: 50px;width: 100px"/>
                                    </el-button>
                                </el-popover>
                            </template>
                        </el-table-column>
                        <el-table-column prop="title" label="名称" width="150"></el-table-column>
                        <el-table-column prop="name" label="指令" width="100"></el-table-column>
                        <el-table-column align="center" prop="money" label="价格" width="80"></el-table-column>
                        <el-table-column prop="shopname" label="店铺" width="200"></el-table-column>
                        <el-table-column align="center" prop="status_text" label="状态" width="70">
                            <template slot-scope="scope">
                                <span v-html="scope.row.status_text"></span>
                            </template>
                        </el-table-column>
                        <el-table-column align="center" prop="created_at" label="日期" width="155"></el-table-column>
                        <el-table-column label="操作" >
                            <template slot-scope="scope">
                                <el-button @click="edit(scope.row)" icon="fa fa-edit" type="primary" size="mini" plain> 编辑</el-button>
                                <el-button v-if="scope.row.status == 1" @click="lock(scope.row.id)" icon="fa fa-close" type="danger" size="mini" plain> 下架</el-button>
                                <el-button v-if="scope.row.status == 2" @click="unlock(scope.row.id)" icon="fa fa-openid" type="primary" size="mini" plain> 上架</el-button>
                                <el-button @click="destory(scope.row.id)" icon="fa fa-trash" type="info" size="mini" plain> 删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <pagination :pageData="listData" :params="params" @list="list"/>
                </div>
            </section>
        </el-main>
        <!-- 表单弹窗-->
        <el-dialog
                v-if="dialog.show"
                :title="dialog.title"
                custom-class="cms-dialog"
                :top="dialog.top"
                :visible.sync="dialog.show"
                :width="dialog.width">
            <el-form :model="form" ref="form" label-width="100px">
                <el-form-item label="店铺：" :rules="[{ required: true, message: '请选择店铺名称', trigger: 'change'}]" prop="shopid">
                    <el-select size="medium" v-model="form.shopid" placeholder="请选择店铺名称">
                        <el-option v-for="(shop,index) in shops" :key="index" :label="shop.name"
                                   :value="shop.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item required :rules="[{ required: true, message: '请上传封面图', trigger: 'blur'}]" label="封面图" prop="thumb">
                    <el-upload
                            class="el-upload-thumb"
                            :action="upload"
                            :headers="myHeaders"
                            :show-file-list="false"
                            name="image"
                            :on-success="uploadSuccess"
                            :before-upload="uploadBefore">
                        <img v-if="form.thumb" :src="form.thumb"
                             class="el-thumb">
                        <i v-else class="el-icon-plus el-upload-thumb-icon"></i>
                    </el-upload>
                </el-form-item>
                <el-form-item label="名称：" :rules="[{ required: true, message: '请填写菜品名称', trigger: 'blur'}]" prop="title">
                    <el-input v-model="form.title"  placeholder="请填写菜品名称"></el-input>
                </el-form-item>
                <el-form-item :rules="[{ required: true, message: '请填写菜品价格', trigger: 'blur'}]" label="价格：" prop="money">
                    <el-input v-model="form.money" placeholder="请填写菜品价格"></el-input>
                </el-form-item>
                <el-form-item v-if="form.id" label="指令：" prop="name">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="简介：" prop="describe">
                    <el-input type="textarea" v-model="form.describe" placeholder="请填写菜品简介"></el-input>
                </el-form-item>
                <el-form-item required label="状态：" prop="status">
                    <el-radio-group v-model="form.status" size="small">
                        <el-radio :label="1" border>上架</el-radio>
                        <el-radio :label="2" border>下架</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialog.show = false">取 消</el-button>
                <el-button type="primary" @click="update()">确 定</el-button>
              </span>
        </el-dialog>
    </el-container>
</template>

<script>
    import {mapGetters} from 'vuex';
    import {FoodList,FoodUpdate,FoodDestroy,FoodLock,FoodUnlock,ShopAll} from '@/utils/request';
    import Pagination from '@/components/pagination';
    export default {
        components: {Pagination},
        data() {
            return {
                listData: [],//列表数据
                params: {page:1,kwd:'',shopid:''},
                dialog: {
                    title: '',
                    top: '10vh',
                    show: false,
                    width: '500px'
                },
                shops:[],
                form:null,
                myHeaders: {
                    'Authorization': 'Bearer ' + this.$storage.get('token'),
                    'Accept': 'application/json'
                },
            }
        },
        activated(){
            if(this.$route.query.shopid){
                this.params.shopid = this.$route.query.shopid;
            }else{
                this.params.shopid = '';
            }
            this.list();
            this.shop();
        },
        computed: {
            ...mapGetters(['active','loading','upload']),
        },
        methods: {
            shop(){
                ShopAll().then((res)=>{
                    if (res.code) {
                        this.$message.error(res.msg);
                    } else {
                        this.shops = res.data;
                    }
                })
            },
            create(){
                this.form = {
                    id: 0,
                    title: '',
                    name: '',
                    money: '',
                    describe: '',
                    shopid: '',
                    thumb: '',
                    status: 1,
                };
                this.dialog.title = '新增菜品';
                this.dialog.show = true;
            },
            //编辑
            edit(info){
                this.form = {
                    id: info.id,
                    title: info.title,
                    name: info.name,
                    money: info.money,
                    describe: info.describe,
                    shopid: info.shopid,
                    thumb: info.thumb,
                    status: info.status,
                };
                this.dialog.title = '编辑菜品';
                this.dialog.show = true;
            },
            //筛选
            search(){
                this.params.page = 1;
                this.list();
            },
            //列表
            list(){
                this.$store.dispatch('setLoading', true);
                FoodList({params: this.params}).then((res)=>{
                    if (res.code) {
                        this.$message.error(res.msg);
                    } else {
                        this.listData = res.data;
                    }
                    this.$store.dispatch('setLoading', false);
                })
            },
            //更新
            update(){
                this.$refs['form'].validate((valid) => {
                    if (valid) {
                        FoodUpdate(this.form).then((res)=>{
                            if(res.code){
                                this.$message.error(res.msg);
                            }else{
                                this.$message({
                                    type: 'success',
                                    duration: '1000',
                                    message: res.msg,
                                    onClose:() =>{
                                        this.dialog.show = false;
                                        this.list();
                                    }
                                });
                            }
                        });
                    }
                });
            },
            //上架
            unlock(id){
                FoodUnlock({id: id}).then((res) => {
                    if (res.code) {
                        this.$message.error(res.msg);
                    } else {
                        this.list();
                    }
                });
            },
            //下架
            lock(id){
                FoodLock({id: id}).then((res) => {
                    if (res.code) {
                        this.$message.error(res.msg);
                    } else {
                        this.list();
                    }
                });
            },
            //删除
            destory(id){
                this.$confirm('确定要删除该店铺信息吗？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    FoodDestroy({id: id}).then((res) => {
                        if (res.code) {
                            this.$message.error(res.msg);
                        } else {
                            this.list();
                        }
                    });
                }).catch(() => {});
            },
            uploadSuccess(res, file) {
                this.form.thumb = res.data;
            },
            uploadBefore(file) {
                const isJPG = file.type === 'image/jpeg' || file.type === 'image/png';
                const isLt2M = file.size / 1024 / 1024 < 1;
                if (!isJPG) {
                    this.$message.error('上传封面图片只能是 JPG | PNG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传封面图片大小不能超过 1MB!');
                }
                return isJPG && isLt2M;
            },
        }
    }
</script>

<style scoped>
    .el-thumb{
        width: 200px;
        height: 150px;
        display: block;
    }
    .el-upload-thumb-icon{
        font-size: 28px;
        color: #8c939d;
        width: 200px;
        height: 150px;
        line-height: 150px;
        text-align: center;
    }
    .el-upload-thumb {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        width: 200px;
        height: 150px;
        line-height: 150px;
        text-align: center;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
</style>
